/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewMensagem.java
 *
 * Created on 19/03/2012, 11:13:41
 */

package com.qp.smsmarketing.view;

import com.itextpdf.text.Document;
import com.qp.smsmarketing.utils.JTextFieldFilter;
import com.qp.smsmarketing.view.mbean.MensagemMBean;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultStyledDocument;

/**
 *
 * @author waldenilson
 */
public class ViewMensagem extends AbstractDialog {

    MensagemMBean gmbean = new MensagemMBean();

    /** Creates new form ViewMensagem */
    public ViewMensagem() {
        initComponents();
        setTitle(".:: Mensagens : SMS-Marketing ::.");
        setLocationRelativeTo(null);
//        tf_nome.setDocument(new JTextFieldFilter(JTextFieldFilter.ALPHA_NUMERIC,50));
        tf_desc.setDocument(new JTextFieldFilter(JTextFieldFilter.ALPHA_NUMERIC,160));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        tf_nome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableMensagem = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tf_desc = new javax.swing.JTextArea();
        bt_novo = new javax.swing.JButton();
        lb_caractere = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        cb_ativo = new javax.swing.JCheckBox();

        setBackground(new java.awt.Color(255, 255, 255));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("MENSAGENS");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/qp/smsmarketing/resource/icon/16X16/apply.png"))); // NOI18N
        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Mensagem.:");

        jLabel2.setText("Titulo.:");

        tableMensagem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Código", "Nome", "Descrição", "Ativa ?"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableMensagem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMensagemMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableMensagem);

        tf_desc.setColumns(20);
        tf_desc.setLineWrap(true);
        tf_desc.setRows(5);
        tf_desc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_descKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tf_descKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tf_desc);

        bt_novo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/qp/smsmarketing/resource/icon/16X16/add.png"))); // NOI18N
        bt_novo.setText("Novo");
        bt_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_novoActionPerformed(evt);
            }
        });

        lb_caractere.setText("Caracteres.: 0 de 160");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/qp/smsmarketing/resource/icon/16X16/cancel.png"))); // NOI18N
        jButton3.setText("Sair");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        cb_ativo.setText("Mensagem Ativa ?");
        cb_ativo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        cb_ativo.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_nome, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                            .addComponent(lb_caractere, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(20, 20, 20))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cb_ativo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                        .addComponent(bt_novo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addGap(30, 30, 30))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lb_caractere)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cb_ativo)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(bt_novo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tableMensagemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMensagemMouseClicked

        gmbean.selecionarRegistro(this);
        
        // TODO add your handling code here:
    }//GEN-LAST:event_tableMensagemMouseClicked

    private void bt_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_novoActionPerformed

        gmbean.novoObjeto(this);
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_novoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        String res = gmbean.salvarObjeto(this);

        if ( res.equals("error") )
            JOptionPane.showMessageDialog
                    (null,"Ocorreu um erro em salvar modificações.\nTente novamente.","Erro",2);
        else if(res.equals("verify"))
            JOptionPane.showMessageDialog
                    (null,"Informe dados corretos.\nTente novamente.","Erro",2);
        else
        {
            gmbean.buscarCrudMensagem(this);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

        
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosed

    private void tf_descKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_descKeyPressed

        if(tf_desc.getText().length() <= 160)
        {
            lb_caractere.setText("Caracteres.: "+tf_desc.getText().length()+" de 160");
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_descKeyPressed

    private void tf_descKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_descKeyReleased

        if(tf_desc.getText().length() <= 160)
        {
            lb_caractere.setText("Caracteres.: "+tf_desc.getText().length()+" de 160");
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_descKeyReleased

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        dispose();
        // TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewMensagem().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_novo;
    private javax.swing.JCheckBox cb_ativo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lb_caractere;
    private javax.swing.JTable tableMensagem;
    private javax.swing.JTextArea tf_desc;
    private javax.swing.JTextField tf_nome;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the tableMensagem
     */
    public javax.swing.JTable getTableMensagem() {
        return tableMensagem;
    }

    /**
     * @param tableMensagem the tableMensagem to set
     */
    public void setTableMensagem(javax.swing.JTable tableMensagem) {
        this.tableMensagem = tableMensagem;
    }

    /**
     * @return the tf_desc
     */
    public javax.swing.JTextArea getTf_desc() {
        return tf_desc;
    }

    /**
     * @param tf_desc the tf_desc to set
     */
    public void setTf_desc(javax.swing.JTextArea tf_desc) {
        this.tf_desc = tf_desc;
    }

    /**
     * @return the tf_nome
     */
    public javax.swing.JTextField getTf_nome() {
        return tf_nome;
    }

    /**
     * @param tf_nome the tf_nome to set
     */
    public void setTf_nome(javax.swing.JTextField tf_nome) {
        this.tf_nome = tf_nome;
    }

    /**
     * @return the bt_novo
     */
    public javax.swing.JButton getBt_novo() {
        return bt_novo;
    }

    /**
     * @param bt_novo the bt_novo to set
     */
    public void setBt_novo(javax.swing.JButton bt_novo) {
        this.bt_novo = bt_novo;
    }

    /**
     * @return the cb_ativo
     */
    public javax.swing.JCheckBox getCb_ativo() {
        return cb_ativo;
    }

}
